package com.tinyideatech.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinyideatech.common.utils.StrUtil;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.SysRole;
import com.tinyideatech.system.entity.SysRoleMenu;
import com.tinyideatech.system.entity.SysUserRole;
import com.tinyideatech.system.mapper.SysRoleMapper;
import com.tinyideatech.system.mapper.SysRoleMenuMapper;
import com.tinyideatech.system.mapper.SysUserRoleMapper;
import com.tinyideatech.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysRoleMenuMapper roleMenuMapper;

//    @Override
//    public List<SysRole> selectListByUserId(Long userId) {
//        return null;
//    }
//
//    @Override
//    public Map<Long, String> getRoleMap() {
//        List<SysRole> roleList = super.list(null);
//        Map<Long, String> roleMap = new TreeMap<>();
//        for(SysRole role: roleList){
//            roleMap.put(role.getId() ,role.getName());
//        }
//        return roleMap;
//    }

    @Override
    public Set<String> selectRoleKeysByUserId(Long userId) {
        return userRoleMapper.selectRoleKeysByUserId(userId);
    }

    @Override
    public void changeStatus(SysRole role) {
        SysRole sysRole = getById(role.getRoleId());
        if(sysRole != null){
            sysRole.setStatus(role.getStatus());
            updateById(sysRole);
        }
    }

    @Override
    public int selectUserCountByRoleId(Long roleId) {
        return userRoleMapper.selectCount(new QueryWrapper<SysUserRole>().eq("role_id", roleId));
    }

    @Override
    public List<SysRoleMenu> setRoleMenuList(SysRole role) {
        Long[] menuIds = StrUtil.toLongArray(",", role.getMenuIds());
        List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
        for (Long menuId : menuIds) {
            SysRoleMenu roleMenu = new SysRoleMenu();
            roleMenu.setRoleId(role.getRoleId());
            roleMenu.setMenuId(menuId);
            list.add(roleMenu);
        }
        return list;
    }

    @Override
    public String getMenuIds(SysRole role) {
        List<SysRoleMenu> roleMenuList = roleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>().eq("role_id", role.getRoleId()));
        List<Long> idList = new ArrayList<>();
        for(SysRoleMenu rm: roleMenuList){
            idList.add(rm.getMenuId());
        }
        return StringUtils.join(idList, ",");
    }

    @Override
    public int checkRoleNameUnique(SysRole role) {
        QueryWrapper qw = new QueryWrapper<SysRole>();
        qw.eq("role_name", role.getRoleName());
        if(role.getRoleId() != null){
            qw.ne("role_id", role.getRoleId());
        }
        return super.count(qw);
    }

    @Override
    public int checkRoleKeyUnique(SysRole role) {
        QueryWrapper qw = new QueryWrapper<SysRole>();
        qw.eq("role_key", role.getRoleKey());
        if(role.getRoleId() != null){
            qw.ne("role_id", role.getRoleId());
        }
        return super.count(qw);
    }

}
